<?php

require PATH_CONTRALLERS . 'PhpMysql.php';

class XitongPage extends MenuPage {

    private $CONN;
    private $way;

    function __construct() {
        $this->CONN = new PhpMysql();
        //new包含的类PhpMysql
        parent::getMenuList($this->CONN);
//        print_r(parent::$main_menu_list);exit;
        //调父类的getMenuList方法，把$this->conn赋给$obj_conn传给getMenuList()
        $this->way = isset($_GET['way']) ? trim($_GET['way']) : '';
    }

    function xitong() {
        include "view/xitong_main.php";
    }

    function huanying() {
        include "view/huanying.php";
    }

    function denglu() {
//        $news_list = $this->conn->sqlInsert('tb_login', array('Fid'=>16,'Fname'=>'潘严至尊','Fage'=>21));  //添加
//        $news_list = $this->conn->sqlDelete('tb_login', 'WHERE Fid>59 AND Fid<68');  //删除
//        $news_list = $this->conn->sqlUpdate('tb_login',array('Fname'=>'魏健康'),'WHERE Fid=5');
//        $news_list = $this->conn->getMoreSelect('tb_admin_menu', '*', 'WHERE Fid<4');  //显示所有
        include "view/denglu.php";
    }

    function menuAdd() {
        if (!$_POST) {
            //上级菜单名称
            $different = $this->CONN->getList('tb_admin_menu', 'Fid,FcateId,FcateIds,Fname', "WHERE FcateId=0", "ORDER BY Fsort");
            include PATH_MODULE . "add_module/menu_add.php";
        } else {
            $pids_arr = isset($_POST['pid']) ? ($_POST['pid']) : array(0);
//            print_r($pids_arr);
            if ($pids_arr[0] > 0) {
                $pids = '0,' . implode(',', $pids_arr); //换成字符串后+0
            } else {
                //当不选择任何级别菜单添加时$pids_arr[0]就等于0，这时候需要$pids_arr[1]也等于0 就可以通过去重复值只留一个0 则添加的就为第一级
                $pids_arr[0] = $pids_arr[1] = 0;
                $pids = implode(',', $pids_arr);
            }


            $pids_arr = explode(',', $pids); //再变回来变成数组
            $pids_arr = array_unique($pids_arr); //清除重复值,然后变成数组
            //添加
            $rs = $this->CONN->Add('tb_admin_menu', array('FcateId' => end($pids_arr), 'FcateIds' => implode(',', $pids_arr), 'Flevel' => count($pids_arr), 'Fname' => $_POST['Fname'], 'Fclass' => $_POST['Fclass'], 'Ffunction' => $_POST['Ffunction'], 'Fway' => $_POST['Fway']));
            //5秒跳转回来
            if ($rs > 0) {
                parent::getMsg('添加成功', URL);
            }
        }
    }

    function menuList() {
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $page = ($page < 1) ? 1 : $page;
        $start = ($page - 1) * 15;

        $keywords = isset($_GET['query']) ? trim($_GET['query']) : '';
        $where = $keywords ? "WHERE Fname LIKE '%$keywords%' OR Fclass LIKE '%$keywords%' OR Ffunction LIKE '%$keywords%'" : '';
        //$where= 宗晓啊啊 ？"WHERE Fname LIKE '%宗晓啊啊%' OR Fclass LIKE '%宗晓啊啊%' OR Ffunction LIKE '%宗晓啊啊%' ： '' ;
        $total = $this->CONN->getTotal('tb_admin_menu', $where);

        $list = $this->CONN->getList('tb_admin_menu', '*', $where, 'ORDER BY FcateId,Fsort', "LIMIT $start,15");

        if ($total > 0) {
            $cate_ids = 0;
            foreach ($list as $ls) {
                $cate_ids .= ',' . $ls['FcateIds'];
                //$cate_ids拿到所有的FcateIds
            }

            $categroy = $this->CONN->getList('tb_admin_menu', 'Fid,Fname', "WHERE Fid IN ($cate_ids)");
            //把Fid和名字都取出来，$categroy=Array ( [0] => Array ( [Fid] => 1 [Fname] => 系统设置 ) [1] => Array ( [Fid] => 2 [Fname] => 全局设置 ) [2] => Array ( [Fid] => 3 [Fname] => 网站管理 ) )
            $category_arr[0] = '顶级菜单';
            //$category_arr[0]与下面无关;
            foreach ($categroy as $ls) {
                $category_arr[$ls['Fid']] = $ls['Fname'];
                //id值等于某个名字，一一对应之后$category_arr[$num]才能用;
            }
////          //尺码与名称
//            $list = $this->CONN->getList('think_shop_products', '*', $where, 'ORDER BY FcateId', "LIMIT $start,15");
//            $size_categroy = array('均码', 'S', 'M', 'L', 'XL');
//            $aaa = array();
//            foreach ($list as $ls) {
//                $aaa[$ls['Fid']] = explode(',', $ls['Fsize']);
//            }
//            $bbb = array();
//            foreach ($aaa as $key => $ls) {
//                foreach ($aaa[$key] as $ls) {
//                    $bbb[$key][$ls] = $size_categroy[$ls];
//                }
//            }
        } else {
            echo "<script type='text/javascript'>alert('没有搜索到相关的记录 。')</script>";
        }


        if ($this->way == 'delete') {
            $rs = $this->CONN->Del('tb_admin_menu', 'Fid');
            header('Location:index.php?C=Xitong&A=menuList&way=S');
        }


        include PATH_MODULE . "add_module/fenyePage.php";


        include PATH_MODULE . "add_module/menu_list.php";
    }

    function menuModify() {
        if (!$_POST) {
            $different = $this->CONN->getList('tb_admin_menu', 'Fid,FcateId,Fname', "WHERE FcateId=0", "ORDER BY Fsort");
            $modify = $this->CONN->View('tb_admin_menu', 'Fid'); //只取一行记录
            $pids_arrr = explode(',', $modify['FcateIds']);
            $total_level = count($pids_arrr);  //统计这个变化的数组中元素的个数以防止$pids_arrr[1]没有值
            if ($total_level == 1) {
                $pids_arrr[1] = 0; //赋值
                $pids_arrr[2] = 0;
                $pids_arrr[3] = 0;
            }
            if ($total_level == 2) {
                $pids_arrr[2] = 0; //赋值
                $pids_arrr[3] = 0;
            }
            if ($total_level == 3) {
                $pids_arrr[3] = 0; //赋值
            }
            print_r($pids_arrr);
            include PATH_MODULE . "add_module/menu_modify.php";
        } else {
            $data = array(
                'Fname' => $_POST['Fname'],
                'Fclass' => $_POST['Fclass'],
                'Ffunction' => $_POST['Ffunction'],
                'Fway' => $_POST['Fway'],
            );
//            //添加
            $rs = $this->CONN->Edit('tb_admin_menu', $data, 'Fid');
            if ($rs > 0) {
                parent::getMsg('修改成功', URL);
            }
        }
    }

    function menuAjax() {
        //接收value值
        $Pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
        $Zid = isset($_GET['zid']) ? intval($_GET['zid']) : 0;
        $tid = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
        //$Level_second[$Pid]是一个数组
        $Level_second[$Pid] = $this->CONN->getList('tb_admin_menu', 'Fid,Fname,FcateId,FcateIds', "WHERE FcateId=$Pid ", "ORDER BY Fsort");
        echo "<option value='0'>请选择下级</option>";
        foreach ($Level_second[$Pid] as $ss) {
            $selected = '';
            if ($ss['Fid'] == $Zid) {
                $selected = "selected='selected'";
            }
            echo "<option value='{$ss['Fid']}' $selected>{$ss['Fname']}</option>";
        }

        echo '|';

        $Level_second[$Zid] = $this->CONN->getList('tb_admin_menu', 'Fid,Fname,FcateId,FcateIds', "WHERE FcateId=$Zid ", "ORDER BY Fsort");
        echo "<option value='0'>请选择下级</option>";
        foreach ($Level_second[$Zid] as $ss) {
            $selected = '';
            if ($ss['Fid'] == $tid) {
                $selected = "selected='selected'";
            }
            echo "<option value='{$ss['Fid']}' $selected>{$ss['Fname']}</option>";
        }
    }

    function menuYes() {
        include PATH_VIEW . "menu_yes.php";
    }

    function cookie() {
        $user_name = isset($_POST['username']) ? trim($_POST['username']) : '';
        $user_pwd = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';
        $useryz = isset($_POST['useryz']) ? trim($_POST['useryz']) : '';
        if (strtoupper($useryz) != strtoupper($_SESSION['yanz'])) {
            echo '0|对不起，验证码输入有误！';
            exit;
        }
        $rs = $this->CONN->getOneRow('tb_admin_users', 'Fid,Fname,Faccount,Fstatus,FmenuLevel,FuserLevel', "WHERE (Faccount='{$user_name}' AND Fpassword='$user_pwd')");
        if (isset($rs['Fid'])) {
            setcookie('user[id]', $rs['Fid'], 0, '/', '.zongxiao.me');
            setcookie('user[name]', $rs['Fname'], 0, '/', '.zongxiao.me');
            header();
        } else {
            echo '0|对不起，您输入的登录信息有误，请重新输入。';
//            echo "<script type='text/javascript'>alert('抱歉，该帐号不存在或密码错误！')</script>";
//            echo "<script type='text/javascript'>location.href = 'http://www.zongxiao.me/';</script>";
        }


//        if ($user_name == $rs[$key]['Faccount'] && md5($user_pwd) == $rs[$key]['Fpassword']) {
//            setcookie('user[id]', $rs[$key]['Fid'], 0, '/', '.zongxiao.me');
//            setcookie('user[name]', $rs[$key]['Fname'], 0, '/', '.zongxiao.me');
////            $_SESSION['user']['user'] = $rs[0]['Fname'];
////            $_SESSION['user']['id'] = $rs[0]['Fid'];
//            header('Location:index.php?C=Xitong&A=huanying');
//        } else {
//            echo "<script type='text/javascript'>alert('抱歉，登录失败！')</script>";
//            echo "<script type='text/javascript'>location.href = 'http://www.zongxiao.me/';</script>";
//        }
        //      include PATH_MODULE . "add_module/cookie.php";
    }

    function loginOut() {
        setcookie('user[id]', '', time() - 10, '/', '.zongxiao.me');
        setcookie('user[name]', '', time() - 10, '/', '.zongxiao.me');
//        unset($_COOKIE['user']);
//        print_r($_COOKIE);
//        exit;
        echo "<script type='text/javascript'>location.href = 'http://www.zongxiao.me/';</script>";
    }

    function echoimg() {
        include PATH_MODULE . "add_module/yanzPage.php";
        $echo = new yanzPage(70, 30, 4);
        $_SESSION['yanz'] = $echo->getfont(); //将验证码放到服务器自己的空间保存一份
        $echo->echotuxiang(); //将验证码图片输出
    }

    function menuFenzu() {
        if ($this->way == 'A') {
            if (!$_POST) {
                $menu_list = $this->CONN->getList('tb_admin_menu', 'Fid,Fname,FcateId', 'ORDER BY Fsort');
                $menu_arr = array();
                foreach ($menu_list as $ls) {
                    $menu_arr[$ls['FcateId']][$ls['Fid']] = $ls['Fname'];
                }
//                print_r($menu_arr);
                include PATH_MODULE . "menu_fenzu/menu_fenzu.php";
            } else {
                $menu_ids = implode(',', $_POST['FmenuIds']);
                $data = array(
                    'FmenuIds' => trim($menu_ids, ','),
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : 1,
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 1
                );
                $rs = $this->CONN->Add('tb_admin_menu_level', $data);
                if ($rs > 0) {
                    parent::getMsg('操作成功', URL);
                } else {
                    parent::getMsg('操作失败', URL);
                }
            }
        }

        if ($this->way == 'S') {
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1; //当前页码
            $pagesize = 15; //每页条数
            $page = ($page < 1) ? 1 : $page;
            $start = ($page - 1) * $pagesize; //起始条数
            //查询关键字
            $keywords = isset($_GET['query']) ? trim($_GET['query']) : '';
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%'" : '';

            //取总记录数
            $total = $this->CONN->getTotal('tb_admin_menu_level', $where);
            $info_list = $this->CONN->getList('tb_admin_menu_level', '*', $where, 'ORDER BY Fsort', "LIMIT $start,$pagesize");

            include PATH_MODULE . "menu_fenzu/fenyyPage.php";

            include PATH_MODULE . "menu_fenzu/guanli_menu.php";
        }

        if ($this->way == 'aa') {
            if (!$_POST) {
                $edit = $this->CONN->View('tb_admin_menu_level', 'Fid');
                if ($edit['Fid'] == $_COOKIE['user']['id']) {
                    $cate_id_arr = explode(',', $edit['FmenuIds']);

                    $menu_list = $this->CONN->getList('tb_admin_menu', 'Fid,Fname,FcateId', 'ORDER BY Fsort');
                    $menu_arr = array();
                    foreach ($menu_list as $ls) {
                        $menu_arr[$ls['FcateId']][$ls['Fid']] = $ls['Fname'];
                    }
                    include PATH_MODULE . "menu_fenzu/fenzu_modify.php";
                } else {
                    parent::getMsg('你没有权限更改其他用户！', 'http://www.zongxiao.me/index.php?C=Xitong&A=menuFenzu&way=S');
                }
            } else {
                $menu_ids = implode(',', $_POST['FmenuIds']);
                $data = array(
                    'FmenuIds' => trim($menu_ids, ','),
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : 1,
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 1
                );
                //修改数据
                $rs = $this->CONN->Edit('tb_admin_menu_level', $data, 'Fid');
                if ($rs !== false) {
                    parent::getMsg('操作成功', URL);
                } else {
                    parent::getMsg('操作失败', URL);
                }
            }
        }


        if ($this->way == 'D') {
            $rs = $this->CONN->Del('tb_admin_menu_level', 'Fid');
            if ($rs !== false) {
                $href = str_replace('&way=D', '&way=S', URL);
                parent::getMsg('操作成功', $href);
                exit;
            } else {
                parent::getMsg('对不起，操作失败', URL);
                exit;
            }
        }
    }

    function myInfo() {
        if ($this->way == 'P') {
            if (!$_POST) {
                $qqq = $_COOKIE['user']['id'];
                $edit = $this->CONN->getOneRow('tb_admin_users', '*', "WHERE Fid=$qqq");
                include PATH_MODULE . "user_guanli/xiugai.php";
            } else {
                $pwd1 = isset($_POST['Fpassword']) ? trim($_POST['Fpassword']) : '';
                $pwd2 = isset($_POST['Fpassword2']) ? trim($_POST['Fpassword2']) : '';
                if ($pwd1 != $pwd2) {
                    echo '0|对不起，你两次输入的密码不一致！';
                } else {
                    header();
                }
                $qqq = $_COOKIE['user']['id'];
                $xiugai = $this->CONN->getOneRow('tb_admin_users', '*', "WHERE Fid=$qqq");
                $xiugai['Fpassword'] = md5($pwd1);
                $rs = $this->CONN->Update('tb_admin_users', $xiugai, 'WHERE Fid=' . $qqq);
            }
        }




        if ($this->way == 'E') {
            if (!$_POST) {
                $USER_ID = $_COOKIE['user']['id'];
                $list = $this->CONN->getOneRow('tb_admin_users', '*', "WHERE Fid=$USER_ID");
//                $list = $this->CONN->View('tb_admin_users', 'Fid'); //只取一行记录
                include PATH_MODULE . "user_guanli/user_modefy.php";
            } else {
                $data = array(
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Femail' => isset($_POST['Femail']) ? trim($_POST['Femail']) : '',
                    'Fqq' => isset($_POST['Fqq']) ? trim($_POST['Fqq']) : '',
                    'Fmobile' => isset($_POST['Fmobile']) ? trim($_POST['Fmobile']) : '',
                    'Ftel' => isset($_POST['Ftel']) ? trim($_POST['Ftel']) : '',
                );
                $rs = $this->CONN->Edit('tb_admin_users', $data, 'Fid');
                if ($rs > 0) {
                    parent::getMsg('操作成功', URL);
                } else {
                    parent::getMsg('操作失败', URL);
                }
            }
        }



        if ($this->way == 'S') {
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1; //当前页码
            $pagesize = 15; //每页条数
            $page = ($page < 1) ? 1 : $page;
            $start = ($page - 1) * $pagesize; //起始条数
            //查询关键字
            $keywords = isset($_GET['query']) ? trim($_GET['query']) : '';
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%' OR Faccount LIKE '%$keywords%' OR Femail LIKE '%$keywords%' OR Fqq LIKE '%$keywords%'" : '';

            //取总记录数
            $total = $this->CONN->getTotal('tb_admin_users', $where);
            $info_list = $this->CONN->getList('tb_admin_users', '*', $where, 'ORDER BY Fid', "LIMIT $start,$pagesize");

            if ($total != 0) {
                $cate_ids = 0;
                foreach ($info_list as $ls) {
                    $cate_ids .= ',' . $ls['FmenuLevel'];
                }
                //取所有菜单，构造数组
                $categroy = $this->CONN->getList('tb_admin_menu_level', 'Fid,Fname', "WHERE Fid IN ($cate_ids)");
                $menu_level_arr = array();
                foreach ($categroy as $ls) {
                    $menu_level_arr[$ls['Fid']] = $ls['Fname'];
                }
                $cate_ids = 0;
                foreach ($info_list as $ls) {
                    if (!empty($ls['FuserLevel'])) {
                        $cate_ids .= ',' . $ls['FuserLevel'];
                    }
                }

                //取所有菜单，构造数组
                $categroy = $this->CONN->getList('tb_admin_user_level_class', 'Fid,Fname', "WHERE Fid IN ($cate_ids)");
                $user_level_arr = array();
                foreach ($categroy as $ls) {
                    $user_level_arr[$ls['Fid']] = $ls['Fname'];
                }
            }
            include PATH_MODULE . "user_guanli/fenyepage.php";

            include PATH_MODULE . "user_guanli/user_list.php";
        }





        if ($this->way == 'A') {
            if (!$_POST) {
                $menu_level = $this->CONN->getList('tb_admin_menu_level', 'Fid,Fname', 'ORDER BY Fsort');
                $user_level = $this->CONN->getList('tb_admin_user_level_class', 'Fid,Fname', 'ORDER BY Fsort');
                include PATH_MODULE . "user_guanli/user_add.php";
            } else {
                $menu_id_arr = isset($_POST['FmenuLevel']) ? $_POST['FmenuLevel'] : array();
                $menu_id_arr = array_unique($menu_id_arr);
                $menu_ids = implode(',', $menu_id_arr);

                $user_id_arr = isset($_POST['FuserLevel']) ? $_POST['FuserLevel'] : array();
                $user_id_arr = array_unique($user_id_arr);
                $user_ids = implode(',', $user_id_arr);

                $data = array(
                    'FmenuLevel' => trim($menu_ids, '0,'),
                    'FuserLevel' => trim($user_ids, '0,'),
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Faccount' => isset($_POST['Faccount']) ? trim($_POST['Faccount']) : '',
                    'Fpassword' => isset($_POST['Fpassword']) ? md5(trim($_POST['Fpassword'])) : '',
                    'Fsex' => isset($_POST['Fsex']) ? intval($_POST['Fsex']) : 0,
                    'Femail' => isset($_POST['Femail']) ? trim($_POST['Femail']) : '',
                    'Fqq' => isset($_POST['Fqq']) ? trim($_POST['Fqq']) : '',
                    'Fmobile' => isset($_POST['Fmobile']) ? trim($_POST['Fmobile']) : '',
                    'Ftel' => isset($_POST['Ftel']) ? trim($_POST['Ftel']) : '',
                    'FaddTime' => time()
                );
                $info = $this->CONN->getOneRow('tb_admin_users', 'Fid,Faccount', "WHERE Faccount='{$data['Faccount']}'");
                if ($_POST['Faccount'] != $info['Faccount']) {

                    $rs = $this->CONN->Add('tb_admin_users', $data);
                    if ($rs > 0) {
                        parent::getMsg('添加用户成功', URL);
                    }
                } else {
                    echo "<script type='text/javascript'>alert('该账户已被人使用，请重新注册！');location.href = 'http://www.zongxiao.me/index.php?C=Xitong&A=myInfo&way=A';</script>'";
                }
            }
        }
        if ($this->way == 'D') {
            $rs = $this->CONN->Del('tb_admin_users', 'Fid');
            if ($rs !== false) {
                $href = str_replace('&way=D', '&way=S', URL);
                parent::getMsg('操作成功', $href);
                exit;
            } else {
                parent::getMsg('对不起，操作失败', URL);
                exit;
            }
        }
    }

}
